<?php
/**
 * Created by PhpStorm.
 * User: zyark
 * Date: 2017/8/28
 * Time: 21:20
 */

namespace Admin\Controller;

use Admin\Model\Page;
use Common\Utils\Constants;

/**
 * 奖品
 * Class PrizeController
 * @package Admin\Controller
 */
class PrizeController extends BaseController
{
    public function __construct($baseModel)
    {
        parent::__construct(D('Prize'));
        $this->bcItemPush('奖品列表', U('Prize/index'));
    }

    private static $_validate = array(
        array('title', 'require', '奖品标题不能为空！', 1, 'regex', 3),
        array('image_url', 'require', '奖品图片不能为空！', 1, 'regex', 3),
        array('type', 'require', '奖品类型不能为空！', 1, 'regex', 3),
        array('probability', 'require', '奖品概率不能为空！', 1, 'regex', 3),
    );

    public function index(){
        $RoomModel = D('Prize');
        //构建查询条件
        $pagesize = 20;
        $id = I('id');
        $type = I('type');
        $title = I('title');
        $pagenumber = I('pagenum', '');
        if (empty($pagenumber) || $pagenumber <= 0)
            $pagenumber = 1;
        if ($id != '') {
            $map['t_prize.id'] = $id;
        }
        if ($id != '') {
            $map['t_prize.title'] = array('like',"%{$title}%");
        }
        if ($type != '' && $type != '-1') {
            $map['t_prize.type'] = $type;
        }
        //查询
        $startno = ($pagenumber - 1) * $pagesize;
        $limit = $startno . ',' . $pagesize;//每页的数据数和内容$limit
        $map['is_delete'] = 0;
        $list = $RoomModel->where($map)->order('id desc')->limit($limit)
            ->field('t_prize.*')->select();
        $recordcount = $RoomModel->where($map)->count();
        //封装分页对象
        $page = new Page($pagenumber, $recordcount, $pagesize);
        $page = (array)$page;//转换为数组
        //显示数据
        $this->assign('page', $page);
        $this->assign('id', $id);
        $this->assign('type', $type);
        $this->assign('title', $title);
        $this->assign('pagenumber', $pagenumber);
        $this->assign('list', $list);
        $this->assign('recordcount', $recordcount);
        $this->display();
    }

    public function addUI()
    {
        $this->bcItemPush('添加奖品', U('Prize/addUI'));
        $this->display('save');
    }

    protected function doAddBefore(&$data)
    {
        parent::doAddBefore($data);
        if($data['type'] != Constants::PRIZE_TYPE_GOLD && $data['type'] !=Constants::PRIZE_TYPE_INTEGRAL){
            $data['number'] = 0;
        }
    }

    public function add()
    {
        parent::add(self::$_validate);
    }

    public function editUI($id)
    {
        $where['id'] = ':id';
        $prize = $this->baseModel->where($where)->bind($where['id'], $id)->find();
        $this->bcItemPush('编辑奖品', U('Prize/editUI/'.$id));
        $this->assign('prize', $prize);
        $this->display('save');
    }

    protected function doEditBefore(&$data)
    {
        if($data['type'] != Constants::PRIZE_TYPE_GOLD && $data['type'] !=Constants::PRIZE_TYPE_INTEGRAL){
            $data['number'] = 0;
        }
    }

    public function edit()
    {
        parent::edit(self::$_validate);
    }

    public function doDeleteBefore($id)
    {
    }

    public function delete($id)
    {
        $race = D('Prize')->where(array('id'=>$id))->find();
        if(empty($race['id']) || $race['is_delete'] == 1){
            $this->ajaxReturn(array('success'=>false,'message'=>'该奖品不能删除！'));
        }
        D('Prize')->where(array('id'=>$id))->setField('is_show',0);//下架
        D('Prize')->where(array('id'=>$id))->setField('is_delete',1);
        $count = D('Prize')->where(array('is_delete'=>1))->count();
        $this->ajaxReturn(array('success'=>true,'message'=>'删除成功！','count'=>$count));
    }

    public function updateField()
    {
        $id = I('id');
        $value = I('value');
        $field = I('field');
        $where['id'] = ':id';
        $bind[':id'] = $id;
        $category = D('Prize')->where($where)->bind($bind)->find();
        $category[$field] = $value;
        $result = D('Prize')->where($where)->bind($bind)->setField($field,$value);
        if($result){
            $this->ajaxReturn(array('success'=>true,'message'=>'修改成功'));
        }else{
            $this->ajaxReturn(array('success'=>false,'message'=>'修改失败'));
        }
    }
}